package com.education.platform.api.service.impl;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.common.enums.AuditTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.ArticleDao;
import com.education.platform.dao.CourseDao;
import com.education.platform.dao.entity.ArticleDO;
import com.education.platform.dao.entity.CourseCommentDO;
import com.education.platform.dao.entity.CourseDO;
import com.education.platform.dao.entity.UserBaseDO;
import com.education.platform.dao.model.CourseCommentModel;
import org.springframework.beans.factory.annotation.Autowired;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.CourseCommentDao;
import org.springframework.stereotype.Service;
import com.education.platform.api.service.CourseCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * describe: 课程评论
 *
 * @author : tomato
 * @date: 2020-06-06 02:55:52.634
 **/
@Service
public class CourseCommentServiceImpl extends BaseServiceImpl<CourseCommentDao, CourseCommentDO> implements CourseCommentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CourseCommentServiceImpl.class);

    @Autowired
    private CourseCommentDao courseCommentDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private ArticleDao articleDao;

    /**
     *  获取精选评论
     * @param commentDO
     * @return
     */
    @Override
    public List<CourseCommentModel> getAuditComment(CourseCommentModel commentDO) {
        return courseCommentDao.getAuditComment(commentDO);
    }

    /**
     * 提交评论
     * @param commentDO
     */
    @Override
    public void commitComment(CourseCommentDO commentDO) {
        if (commentDO == null || commentDO.getTopicType() == null || commentDO.getCourseId() == null) {
            throw new BusinessException("数据为空");
        }
        if (commentDO.getTopicType() == 1) {
            CourseDO courseDO = courseDao.getById(commentDO.getCourseId());
            if (courseDO == null) {
                throw new BusinessException("评论对象不存在");
            }
            commentDO.setTopicTitle(courseDO.getCourseName());
        } else if (commentDO.getTopicType() == 2) {
            ArticleDO articleDO = articleDao.getById(commentDO.getCourseId());
            if (articleDO == null) {
                throw new BusinessException("评论对象不存在");
            }
            commentDO.setTopicTitle(articleDO.getTitle());
        }
        UserBaseDO userBaseDO = ThreadUser.getUser(UserBaseDO.class);
        commentDO.setUserId(userBaseDO.getId());
        commentDO.setUserCover(userBaseDO.getPhoto());
        commentDO.setUserName(ThreadUser.getUserName());
        commentDO.setCreateTime(new Date());
        commentDO.setReadStatus(false);
        commentDO.setAuditStatus(AuditTypeEnum.NOT_AUDIT.getValue());
        courseCommentDao.insert(commentDO);
    }
}
